clear all
set type double
version 18.5
sysdir set PLUS ado_rdrobust920/plus

set seed 9
cap noisily mkdir ../fishertest


cap program drop hist_fisher
program hist_fisher 	
	version 16.1
	set seed 1
	set type double
	pause on 
	do modules/__var_labeling.do
	args stat filename v1 v2 graph_title graph_name hist_scatter

	use ../fishertest/`filename', clear
	merge 1:1 com_n using ../fishertest/balance.dta
	keep if com_n < 8193 & ttest_w > .05 & ttest_p > .05 & ttest_m > .05

	varlabeling
	local GRAPH = ""
	local graphtitle = ""
	local savefilename = ""
	foreach var in "`v1'" "`v2'"{
		if "`var'" != ""{
			di "`var'"
			local bidder_auction = "auction"
			if ustrregexm("`var'", "^(?:q_|pctbid_|abovereserve_)"){
				local bidder_auction = "bidder"
			}
			cap graph drop dff2`var'
			di "***** dff_`stat'_`var' *****"
			qui count if dff2_`stat'_`var' < .
			local N_patterns = r(N)		/* added on 10oct22*/
			di "obs_all: `N_patterns'"
			
			sort dff2_`stat'_`var' com_n
			gen ranking = _n
			sum ranking if DB_binary == "0101100011111", mean
			local ranking = r(mean)
			local target_value = dff2_`stat'_`var'[`ranking']
			qui count if dff2_`stat'_`var' <= dff2_`stat'_`var'[`ranking']
			local my_ranking = r(N)
			di "ranking: `my_ranking'"
			drop ranking

			local p_`var' = string(`my_ranking'/`N_patterns', "%6.3f")
			di "p_`var': `p_`var''"

			if "`hist_scatter'" == "hist"{
				hist dff2_`stat'_`var', bin(200) xline(`target_value',lc(black)) color(gs8%20) name(dff2`var')  ///
				subtitle("Quantile: `p_`var''") graphregion(color(white)) ylabel(,angle(0)) 
				local GRAPH = "`GRAPH' dff2`var'"
				local graphtitle = "`graphtitle' `var'"
				local savefilename = "`savefilename'_`var'"
			}
			else{
				qui gen p_`var' = _n
				qui replace p_`var' = p_`var' / `N_patterns'
			}
		}
	}
	if "`hist_scatter'" == "hist"{
		cap graph drop `stat'_`v1'`v2'
		if "`v2'" != ""{
			graph combine `GRAPH', xsize(4) ysize(2) name(`graph_name') graphregion(color(white)) scale(1.2)
		}
		else{
			graph combine `GRAPH', xsize(2) ysize(2) name(`graph_name') graphregion(color(white)) scale(.8)
		}
		graph export ../graph/diffdiff`savefilename'_`bidder_auction'_`stat'`q'.png, width(1296) replace
	}
	else{
		gen y_2 = 1
		gen y_1 = (p_`v2'-`p_`v2'') + `p_`v1''
		di "****************** points below 45 degree line *******************"
		count if p_`v1' < . & p_`v2' < .
		count if p_`v1' >= (p_`v2'-`p_`v2'') + `p_`v1''
		count if p_`v1' > `p_`v1'' & p_`v2' < `p_`v2'' & p_`v1'< .
		di "******************************************************************"
		twoway (rarea y_1 y_2 p_`v2' if y_1 <= 1, color(gs4%30)) ///
		(scatter p_`v1' p_`v2', yline(`p_`v1'',lc(black)) xline(`p_`v2'', lc(black)) color(gs3%40) mlstyle(none)  ///
		/*subtitle("p-value: `dff2_treatment_control_p'") name(dff2`var')*/ graphregion(color(white) margin(r+10 t+5)) ///
		ytitle("Price") xtitle("Score") text(1.05 `p_`v2'' "`p_`v2''", size(small)) ylabel(,angle(0)) ///
		legend(off) text(`p_`v1'' 1.08 "`p_`v1''", size(small)) xsize(10) ysize(9) name(`graph_name'))
		graph export ../graph/fisher_scatter_`savefilename'_`bidder_auction'_`stat'`q'.png, width(1296) replace
	}
end

/**/
/* assigning pair id form 1 to 13*/
	clear
	set obs 28
	gen g28_plus = _n
	*北海道
	gen pair28 = 1 if g28_plus == 1 | g28_plus == 7 		/*A: 道央南（14）,紋別・網走	31*/	
	replace pair28 = 2 if g28_plus == 6 | g28_plus == 8 	/*B: 釧路・根室	14,小樽・ニセコ	14*/
	replace pair28 = 3 if g28_plus == 2 | g28_plus == 3 	/*C: 道央北	8,函館松前	10*/
	replace pair28 = 4 if g28_plus == 4 | g28_plus == 5 	/*D: 留萌	4,稚内	4*/
	*造園
	replace pair28 = 5 if g28_plus == 9 | g28_plus == 14 	/*E: 茨城造園	10,大分造園	5*/
	replace pair28 = 6 if g28_plus == 26 | g28_plus == 27 	/*F: 福井造園	1,釧路造園	3*/
	*西日本道路
	replace pair28 = 7 if g28_plus == 12 | g28_plus == 22 	/*G: 島根道路	3,愛媛道路	4*/
	replace pair28 = 8 if g28_plus == 18 | g28_plus == 25 	/*H: 岐阜標識	2,四国道路	1*/
	
	*あまり、ringirmsの数の多い順に割り振り020319
	replace pair28 = 9 if g28_plus == 10 | g28_plus == 16 	/*I: 山形	32,静岡三島	24,数が多い*/
	replace pair28 = 10 if g28_plus == 11 | g28_plus == 19 	/*J: 秋田	11,愛知・岐阜	18*/
	replace pair28 = 11 if g28_plus == 17 | g28_plus == 20 	/*K: 岐阜・長野	8,静岡菊川	6*/
	replace pair28 = 12 if g28_plus == 23 | g28_plus == 28 	/*L: 青森	3,徳島	6*/
	replace pair28 = 13 if g28_plus == 13 | g28_plus == 15 	/*M: 岐阜電気	2,新潟	2*/
	save ../fishertest/group28_pair.dta, replace
	/*treatment: 	*0101100011111*/
	/*所属が1社ずつしかいない21と24（21には赤木工業（株）_奈良県大和郡山市今国府町２８８－１、24//
	に森下建設（株）_島根県江津市桜江町市山５４３―１）は対象から除外した*/

cap mkdir ../fishertest

*1) balance test 
/* to drop pattern that does not blance treatment-control using dta of 082821 that includes auction in which q is missing or zero and score is zero*/
/* need egenmore */
/**/
do modules/__balance_test2.do
balance "auction"
/**/


*2) generate pattern
/**/
qui do "modules/rdbwselect2a.ado"
use ../fishertest/group28_pair.dta, replace
qui gen group_id_min = .
qui gen group_id_max = .
forvalues x = 1(1)13{
	qui sum g28_plus if pair28 == `x'
	qui replace group_id_min = `r(min)' if _n == `x'
	qui replace group_id_max = `r(max)' if _n == `x'
}
set obs 8192
gen com_n = _n
gen com_group = ""
sort com_n
qui gen DB_decimal = _n - 1 if _n <= 8192
qui egen DB_binary = base(DB_decimal)  /*** ssc install egenmore if necessary ****/

gen DB_binary_opposite = usubinstr(DB_binary, "0", "2",.)
replace DB_binary_opposite = usubinstr(DB_binary_opposite, "1", "0",.)
replace DB_binary_opposite = usubinstr(DB_binary_opposite, "2", "1",.)

set type double /* 17aug22 necessary for grouping 4151 patterns by DB_max; otherwise only max_DB creates only 131 groups*/
destring DB_binary, gen(DB_binary_real)
destring DB_binary_opposite, gen(DB_binary_opposite_real)
gen com_n_opposite = 8192 - com_n

forvalues m = 1(1)8192{
	sort com_n
	local thebinary = DB_binary[`m']
	*di as result strofreal(DB_decimal[`m']) + ": " _continue
	local S = ""
	forvalues x = 1(1)13{
		local bi = substr("`thebinary'", `x',1)
		*di "`bi'" _continue
		if "`bi'" == "0"{
			local S = "`S' " + strofreal(group_id_min[`x'])
		}
		else{
			local S = "`S' " + strofreal(group_id_max[`x'])
		}
	}
	local S = ustrregexra("`S'", "^ ","")
	*di "; `S'" /*[`c(current_time)' `c(current_date)'; begins at `start_time']"*/
	*computation "`MODE'" "`PERIOD'" "& random_treatment == `t'" "if com_n == `m'" "rdrobust_on"
	qui replace com_group = "`S'" if com_n == `m'
}
keep com* DB_*
save ../fishertest/8192patterns_g28.dta, replace
/**/

*3) compute rd estimation by group 
pause on
qui do modules/rdbwselect2a.ado
qui do modules/__rd_for_fisher_test.do
use ../tmp/sample_090122.dta, clear

get_rd_teststat_for_fisher_test "auction" /*drop less than->*/ /*"p25"*/


/*4) compute mean pctbid quality and prob of above reserve*/
/**/
use ../tmp/sample_090122.dta, clear
gen winner01 = (winner == 1)
rename percent_reserve pctbid
rename pt_tech q /*if pt_tech >= 100 & pt_tech < 200*/
gen abovereserve = pctbid > 1 

global vars = ""
foreach v in "pctbid" "q" "abovereserve" {
	di "`v'"
	foreach winlose in "w" "l"{
		qui gen `v'_`winlose' = `v' if winner01 == ("`winlose'" == "w")
		global vars = "$vars `v'_`winlose'"
	}
}
do modules/__mean_median_for_fisher_test.do
get_mean_median_for_fisher_test "bidder" /*"p25"*/
/**/



/*********************** compute statistics by group *********************/	
set type double 
set seed 1234
use ../fishertest/mean_bidder.dta, clear
merge 1:1 condition_score using ../fishertest/rd_auction.dta
* *) compute groupwise difference/ aggregate 13 groups to compute mean/ weighted mean and median for every 8192 pattern
keep g28_plus condition_* tau* tstat* N* Mean*
sort g28_plus
gen after = ustrregexm(condition_score, "after")
reshape wide tau_score tstat_score tau_price tstat_price condition_* N_* Mean_*, i(g28_plus) j(after)
merge 1:1 g28_plus using ../fishertest/group28_pair.dta
drop if _merge == 2
drop _merge

qui ds
foreach v in `r(varlist)'{
	di "`v'"
	local w = usubinstr("`v'", "Mean", "mean",.)
	local w = usubinstr("`w'", "Median", "median",.)
	cap rename `v' `w'
}
foreach y in "tstat" "tau" $vars {
	if ("`y'" == "tstat" | "`y'" == "tau"){
		foreach runvar in "price" "score"{
			qui gen `y'_`runvar' = `y'_`runvar'1 - `y'_`runvar'0
			cap gen N_`y'_`runvar' = N_`runvar'1 + N_`runvar'0
		}
	}
	else{
		foreach stat in "mean" /*"median"*/ {
			qui gen `stat'_`y' = `stat'_`y'1 - `stat'_`y'0
			cap gen N_`stat'_`y' = N_`y'1 + N_`y'0
		}
	}
}
drop condition_score* condition_price* pair28
qui ds
foreach v in `r(varlist)'{
	di "`v'"
	if ustrregexm("`v'", "(.+)0$"){
		local w = ustrregexra("`v'", "(.+)0$", "$1_before")
		rename `v' `w'
	}
	else if ustrregexm("`v'", "(.+)1$"){
		local w = ustrregexra("`v'", "(.+)1$", "$1_after")
		rename `v' `w'
	}
}
tostring g28_plus, gen(g28_plus_string)
gen com_n = _n
save ../fishertest/statistics_by_group.dta, replace

merge 1:1 com_n using  ../fishertest/8192patterns_g28.dta
drop _merge
local counter = 1
pause on
cap drop dff_*

forvalues i = 1(1)4096{
	local i_oppose = 8193 - `i'
	sort com_n
	local A = com_group[`i']
	local AA = usubinstr("`A'", " ","|",.)
	local B = DB_binary[`i']
	local A_opposite = com_group[`i_oppose']
	local AA_opposite = usubinstr("`A_opposite'", " ","|",.)
	local B_opposite = DB_binary[`i_oppose']
	*di "[`i']:`AA'; `B'/[`i_oppose']:`AA_opposite'; `B_opposite';" _continue
	qui gen themarkets = ustrregexm(g28_plus_string, "^(?:`AA')$") if g28_plus < .
	*pause
	foreach v in "tstat" "tau" $vars {
		*di "`v'"
		foreach stat in "mean" /*"t_ols" "B_ols"*/ {
			if ("`v'" == "tstat" | "`v'" == "tau"){
				if ("`stat'" == "mean"| "`stat'" == "median"){
					foreach runvar in "score" "price"{
						cap gen dff_`stat'_`v'_`runvar' = .
						sum `v'_`runvar' if themarket == 0, mean
						qui replace dff_`stat'_`v'_`runvar' = r(mean) if com_n == 8193 - `i'
						sum `v'_`runvar' if themarket == 1, mean
						qui replace dff_`stat'_`v'_`runvar' = r(mean) if com_n == `i'
					}
				}
			}
			else {
				cap gen dff_`stat'_`v'= .
				sum `stat'_`v' if themarket == 0, mean
				qui replace dff_`stat'_`v' = r(mean) if com_n == 8193 - `i'
				sum `stat'_`v' if themarket == 1, mean
				qui replace dff_`stat'_`v' = r(mean) if com_n == `i'	
			}
		}
	}
	qui drop themarkets*
	*di ""
}
set type double
 egen max_DB = rowmax(DB_decimal com_n_opposite	)
sort max_DB com_n
order max_DB

foreach r in "score" "price"{
	qui by max_DB: gen dff2_mean_tstat_`r' = dff_mean_tstat_`r' - dff_mean_tstat_`r'[_n-1] if _n == 2
	qui by max_DB: replace dff2_mean_tstat_`r' = dff_mean_tstat_`r' - dff_mean_tstat_`r'[_n+1] if _n == 1
	qui by max_DB: gen dff2_mean_tau_`r' = dff_mean_tau_`r' - dff_mean_tau_`r'[_n-1] if _n == 2
	qui by max_DB: replace dff2_mean_tau_`r' = dff_mean_tau_`r' - dff_mean_tau_`r'[_n+1] if _n == 1
}
foreach v in $vars  {
	foreach st in "mean_" /*"median_"*/ {
		qui by max_DB: gen dff2_`st'`v' = dff_`st'`v' -  dff_`st'`v'[_n-1] if _n == 2
		qui by max_DB: replace dff2_`st'`v' =  dff_`st'`v' -  dff_`st'`v'[_n+1] if _n == 1	
	}
}
drop N_* tau_* tstat_* mean_* g28_plus*
save ../fishertest/beta_hat.dta, replace
/**/
/* 2d graph */
*	args stat filename v1 v2 graph_title q hist_scatter
cap mkdir ../graph
cap graph drop scatter
hist_fisher mean beta_hat tau_price tau_score "{it:{&beta}} of RD" "scatter" "scatter" /*"_p25"*/

/* histogram */
cap graph drop beta
hist_fisher mean beta_hat tau_score tau_price "{it:{&beta}} of RD" "beta" "hist" /*"_p25"*/
/**/
cap graph drop quality
hist_fisher mean beta_hat q_w q_l "Raw Quality" "quality" "hist"
cap grpah drop pctbid
hist_fisher mean beta_hat pctbid_w pctbid_l "Percentage Bids" "pctbid" "hist"
cap graph drop invalidbid
hist_fisher mean beta_hat abovereserve_l "" "Above Reserve" "invalidbid" "hist"

version
which rdrobust
